﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using org.in2bits.MyXls;
using ReflectionUtilHelpV2;

namespace OfficeUtilHelpV2
{
    public class MyxlsToolV2
    {

        public static void ToExecel<T>(IList<T> excelDbSource, string savePath, string sheetName) where T : class
        {
            try
            {
                if (excelDbSource == null)
                    throw new ArgumentNullException("excelDbSource");
                if (string.IsNullOrEmpty(savePath) || !savePath.ToLower().Contains(""))
                    throw new ArgumentNullException("savePath");
                if (string.IsNullOrEmpty(sheetName))
                    throw new ArgumentNullException("sheetName");

                int _recordCnt = excelDbSource.Count;
                XlsDocument _xls = new XlsDocument();
                string _savePath = savePath.Substring(0, savePath.LastIndexOf(@"\") + 1);
                _xls.FileName = savePath.Substring(savePath.LastIndexOf(@"\") + 1);

                // 列标题样式
                XF _colXF = _xls.NewXF(); // 为xls生成一个XF实例，XF是单元格格式对象
                _colXF.HorizontalAlignment = HorizontalAlignments.Centered;
                _colXF.VerticalAlignment = VerticalAlignments.Centered;
                _colXF.UseBorder = true;
                _colXF.TopLineStyle = 1;
                _colXF.TopLineColor = Colors.Grey;
                _colXF.BottomLineStyle = 1;
                _colXF.BottomLineColor = Colors.Grey;
                _colXF.LeftLineStyle = 1;
                _colXF.LeftLineColor = Colors.Grey;
                _colXF.Pattern = 1; // 单元格填充风格。如果设定为0，则是纯色填充(无色)，1代表没有间隙的实色
                _colXF.PatternBackgroundColor = Colors.White;
                _colXF.PatternColor = Colors.Silver;
                _colXF.Font.Bold = true;
                _colXF.Font.Height = 12 * 20;

                // 数据单元格样式
                XF _dataXF = _xls.NewXF(); // 为xls生成一个XF实例，XF是单元格格式对象
                _dataXF.HorizontalAlignment = HorizontalAlignments.Centered;
                _dataXF.VerticalAlignment = VerticalAlignments.Centered;
                _dataXF.UseBorder = true;
                _dataXF.LeftLineStyle = 1;
                _dataXF.LeftLineColor = Colors.Black;
                _dataXF.BottomLineStyle = 1;
                _dataXF.BottomLineColor = Colors.Black;
                _dataXF.UseProtection = false; // 默认的就是受保护的，导出后需要启用编辑才可修改
                _dataXF.TextWrapRight = true; // 自动换行

                Worksheet _sheet = _xls.Workbook.Worksheets.Add(sheetName);
                int _nCelIndex = 0, _nRowIndex = 1;
                Cells _cells = _sheet.Cells;
                IDictionary<string, string> _fields = ReflectionToolV2.GetDisplayName<T>();
                string[] _columns = new string[_fields.Count];
                _fields.Values.CopyTo(_columns, 0);
                foreach (string col in _columns)
                {
                    _nCelIndex++;
                    _cells.Add(1, _nCelIndex, col, _colXF);
                }
                foreach (T t in excelDbSource)
                {
                    _nRowIndex++;
                    _nCelIndex = 0;
                    foreach (KeyValuePair<string, string> entry in _fields)
                    {
                        _nCelIndex++;
                        object _value = typeof(T).InvokeMember(entry.Key, BindingFlags.GetProperty, null, t, null);
                        _cells.Add(_nRowIndex, _nCelIndex, _value == null ? string.Empty : _value.ToString(), _dataXF);
                    }
                }
                _xls.Save(_savePath, true);
             
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ToExcel Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
